package com.example.xymarket.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.xymarket.R;
import com.example.xymarket.po.Goods;
import com.example.xymarket.httpUtil.AsyncImageLoader;
import com.example.xymarket.common.MyUrl;


import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ShopAdapter extends BaseAdapter {

    public List<Goods> items;
    Context context;
    int count = 0;
    public HashMap<Integer, Drawable> hm;
    Handler handlerOfParent;
    int layout= R.layout.item_my_shop;

    public ShopAdapter(List<Goods> items, Context context,Handler handler) {
        super();
        this.items = items;
        handlerOfParent=handler;
        hm=new HashMap<>();
        this.context = context;
        count = items.size();
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup parent)  {
        try {
            Goods goods = items.get(position);
            String url = MyUrl.ImagesUrl + goods.getImage();
            MyViewHolder viewHolder = new MyViewHolder();
            convertView = LayoutInflater.from(context).inflate(
                    layout, null);
            // 初始化 MyViewHolder 方便重用
            viewHolder.id = goods.getId();
            viewHolder.name = (TextView) convertView.findViewById(R.id.order_name);
            viewHolder.name.setText(goods.getTitle());
            viewHolder.sold = (TextView) convertView.findViewById(R.id.merchant_dis);
            viewHolder.sold.setText("月销：" + String.valueOf(goods.getSoldQty()));
            viewHolder.stock = (TextView) convertView.findViewById(R.id.stock);
            viewHolder.stock.setText("库存：" + String.valueOf(goods.getStockQty()));
            viewHolder.price = (TextView) convertView.findViewById(R.id.order_price);
            viewHolder.price.setText(String.valueOf(goods.getPrice() / 100) + "." + String.valueOf(goods.getPrice() % 100));
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.order_avatar);
            viewHolder.update = (Button) convertView.findViewById(R.id.update);
            viewHolder.update.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    update_goods(viewHolder.imageView,goods);
                }
            });
            if(hm.get(position)!=null)
            {
                viewHolder.imageView.setImageDrawable(hm.get(position));
            }
            else
            {
                AsyncImageLoader asyncImageLoader = new AsyncImageLoader();
                asyncImageLoader.loadImage(url, viewHolder.imageView,hm,position);
            }
            convertView.setTag(viewHolder);
            return convertView;
        }
        catch (Exception e)
        {
            return null;
        }
    }

    @SuppressLint("HandlerLeak")
    final Handler handler = new Handler()
    {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1) {
                Toast.makeText(context,"添加成功!",Toast.LENGTH_LONG).show();
            }
        }
    };


    void update_goods(ImageView imageView,Goods goods)
    {
        Map<String,Object> map=new HashMap<>();
        map.put("id",goods.getId());
        map.put("name",goods.getTitle());
        map.put("price",goods.getPrice());
        map.put("stock",goods.getStockQty());
        imageView.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(imageView.getDrawingCache());
        imageView.setDrawingCacheEnabled(false);
        map.put("imageView",bitmap);
        Message msg=new Message();
        msg.obj=map;
        msg.what=3;
        handlerOfParent.sendMessage(msg);

    }

    public final class MyViewHolder {
        int id;
        TextView name;
        TextView sold;
        TextView price;
        TextView stock;
        ImageView imageView;
        Button update;
    }
}

